package com.it.App.entity;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: CaoYouGen
 * @DateTime: 2023/11/22/12:59
 * @注释: TODO
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class LoginUser implements UserDetails {
    // 引入我们的sysUser实体类
    private SysUser sysUser;
    // 权限
    private List<String> auths;

    // 定义一个新的权限集合
    List<SimpleGrantedAuthority> newAuths;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 如果 newAuths 为空 第一个进来需要转换 如果不是直接返回
        if (ObjectUtil.isNull(newAuths)){
            // 将String类型的权限转成SimpleGrantedAuthority类型
            newAuths = auths.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        }
        return newAuths;
    }

    @Override
    public String getPassword() {
        // 返回密码后将密码置空
        String password = sysUser.getPassword();
        sysUser.setPassword(null);
        return password;
    }

    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
